package com.ly.lypopupwindowdemo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ly.lypopupwindowdemo.R;
import com.ly.lypopupwindowdemo.bean.SingleBean;

import java.util.List;

/**
 * Created by liying on 2017/12/28.
 */

public class SingleAdapter3 extends RecyclerView.Adapter<SingleAdapter3.MyHolder> {

    private List<SingleBean> lists;
    private int mSelectedPos = -1;//增加一个变量，保存当前选中的position
    private Context mContext;
    private LayoutInflater mInflater;
    RecyclerView mRv;

    public SingleAdapter3(List<SingleBean> lists, Context mContext,RecyclerView mRv) {
        this.lists = lists;
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
        this.mRv = mRv;

        ///方法3和方法2这里是一样的，也是用mSelectedPos搞事情： 设置数据集时，找到默认选中的pos
        for (int i = 0; i < lists.size(); i++) {
            if (lists.get(i).isSelected()) {
                mSelectedPos = i;
            }
        }
    }

    @Override
    public SingleAdapter3.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_single, parent, false);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final SingleAdapter3.MyHolder holder, final int position) {
        final SingleBean bean = lists.get(position);
        holder.singer.setText(bean.getSinger());
        holder.songname.setText(bean.getSongname());
        holder.radioButton.setSelected(bean.isSelected());

        holder.single_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                MyHolder myHolder = (MyHolder) mRv.findViewHolderForLayoutPosition(mSelectedPos);

                if (myHolder!=null){//还在屏幕里
                    myHolder.radioButton.setSelected(false);
                }else{
                    notifyItemChanged(mSelectedPos);
                }
                lists.get(mSelectedPos).setSelected(false);//不管在不在屏幕里都需要改变数据
                //设置新的Item勾选状态
                mSelectedPos = position;
                lists.get(mSelectedPos).setSelected(true);
                holder.radioButton.setSelected(true);

            }
        });
    }

    @Override
    public int getItemCount() {
        return lists.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        RelativeLayout single_rl;
        private ImageView radioButton;
        private TextView songname;
        private TextView singer;

        public MyHolder(View itemView) {
            super(itemView);
            radioButton = (ImageView) itemView.findViewById(R.id.ratioBtn);
            songname = (TextView) itemView.findViewById(R.id.songname);
            singer = (TextView) itemView.findViewById(R.id.singer);
            single_rl = (RelativeLayout) itemView.findViewById(R.id.single_rl);
        }
    }
}
